package com.example.core.utils;

import com.example.security.service.SysTokenService;
import com.example.sys.org.service.SysUserRoleService;

public class UserUtils {

    private static SysTokenService sysTokenService;

    public static SysTokenService getSysTokenService() {
        if (sysTokenService == null) {
            sysTokenService = (SysTokenService) SpringBeanUtils.getBean(SysTokenService.class);
        }
        return sysTokenService;
    }

    /***
     * 获取当前用户Id
     * @return
     */
    public static String getUserId() {
        return getSysTokenService().getUser() == null ? null : getSysTokenService().getUser().getId();
    }

    /***
     * 获取当前用户名称
     * @return
     */
    public static String getUserName() {
        return getSysTokenService().getUser() == null ? null : getSysTokenService().getUser().getName();
    }

    /***
     * 获取当前用户的部门id
     * @return
     */
    public static String getDeptId() {
        return getSysTokenService().getUser() == null ? null : getSysTokenService().getUser().getParentId();
    }


    /***
     * 获取当前用户的部门名称
     * @return
     */
    public static String getDeptName() {
        return getSysTokenService().getUser() == null ? null : getSysTokenService().getUser().getParentName();
    }

    /***
     * 判断用户是否有角色信息
     * @param userId
     * @param role
     * @return
     */
    public static boolean hasRole(String userId, String role) {
        return ((SysUserRoleService) SpringBeanUtils.getBean(SysUserRoleService.class)).hasRole(userId, role);
    }

    /***
     * 判断当前用户是否有角色
     * @param role
     * @return
     */
    public static boolean hasRole(String role) {
        return ((SysUserRoleService) SpringBeanUtils.getBean(SysUserRoleService.class)).hasRole(getUserId(), role);
    }
}
